﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace HindernisGame 
{
    public class GameOverScreen {
        private Texture2D deinName, deinePunkte, background, gameoverBtn;
        private Game1 game;
        private KeyboardState currentState;
        private Keys[] lastPressedKeys, allowedKeys;
        private SpriteBatch spriteBatch;
        private Rectangle mainFrame;
        private List<menuButton> mbs = new List<menuButton>();
        private int score;
        private int sperre = 0;
        private int maxCharsPerName = 10;
        private String name = "";

        public GameOverScreen(Game1 game)
        {
            this.game = game;
            lastPressedKeys = new Keys[0];
            allowedKeys = new Keys[] {  Keys.A, Keys.B, Keys.C, Keys.D, Keys.E, Keys.F, Keys.G, Keys.H, Keys.I, Keys.J, Keys.K, Keys.L, Keys.M,
                                        Keys.N, Keys.O, Keys.P, Keys.Q, Keys.R, Keys.S, Keys.T, Keys.U, Keys.V, Keys.W, Keys.X, Keys.Y, Keys.Z, 
                                        Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9, Keys.D0 };

            deinePunkte = game.Content.Load<Texture2D>("menu/deinepunkte");
            deinName = game.Content.Load<Texture2D>("menu/deinname");
            gameoverBtn = game.Content.Load<Texture2D>("menu/gameoverBTN");

            mbs.Add(new menuButton(new Rectangle(100, 420, 318, 36), deinePunkte, 0, false));
            mbs.Add(new menuButton(new Rectangle(100, 520, 260, 36), deinName, 1, false));
            mbs.Add(new menuButton(new Rectangle(100, 100, 659, 100), gameoverBtn, 2, false));     
        }

        public void LoadContent()
        {
            spriteBatch = new SpriteBatch(game.GraphicsDevice);
            background = game.Content.Load<Texture2D>("textures/Gameover");
            mainFrame = new Rectangle(0, 0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
        }
 
        public void Update(int score)
        {
            this.score = score;
            //if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
              //  game.Exit();

            if (sperre > 0)
                sperre--;
            else
            {
                currentState = Keyboard.GetState();
                if (currentState.IsKeyDown(Keys.Enter))
                {
                    sperre = 15;
                    if (name.Length < 1) name = "unknown";
                    game.highscoreStruct.SaveHighScore(score, name);
                    game.setCurrentScreen("highscoreScreen");      
                }
                if (currentState.IsKeyDown(Keys.Back))
                {
                    sperre = 5;
                    if (name.Length > 0)
                        name = name.Substring(0, name.Length - 1);
                }
            }
            //=================Texteingabe=====================

            Keys[] pressedKeys = currentState.GetPressedKeys();

            //check if any of the previous update's keys are no longer pressed
            foreach (Keys key in lastPressedKeys)
            {
                if (!pressedKeys.Contains(key))
                    OnKeyUp(key);
            }

            //check if the currently pressed keys were already pressed
            foreach (Keys key in pressedKeys)
            {
                if (!lastPressedKeys.Contains(key))
                    OnKeyDown(key);
            }

            lastPressedKeys = pressedKeys;
        }


        private void OnKeyDown(Keys key)
        {
            if (allowedKeys.Contains(key))
            {
                //if (name.Length <= 1) name = "unknown";
                if (name.Length > maxCharsPerName)
                    name = name.Substring(0, name.Length - 1) + key;
                else
                    name += key;
            }
        }

        private void OnKeyUp(Keys key)
        {
            //do stuff
        }

        public void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(background, mainFrame, Color.White);
            spriteBatch.DrawString(game.font2, score.ToString(), new Vector2(500, 410), Color.White);
            spriteBatch.DrawString(game.font2, name, new Vector2(500, 510), Color.White);
            foreach (menuButton m in mbs)
            {
                m.Draw(spriteBatch, game.font);
            }
            spriteBatch.End();
        }
    }
}
